<?php
require_once ('../config/path_config.php');
require_once(ROOTDIR."admin/admin-header.php");
require_once(ROOTDIR."common/db_utils.php");
require_once(ROOTDIR."include/check_get_key.php");
// 老师和管理员允许进入该页面
// 1. 管理员允许删除任何提交记录
// 2. 老师允许删除自己所教学生和自己的提交记录(未完善)
$allowAccess = isset($_SESSION['administrator']);
$allowAccess |= isset($_SESSION['teacher']);

if (!$allowAccess ){
    echo "<h3>不具备该权限</h3>";
    exit(1);
}

if ( !isset($_GET['solution_id']) || intval($_GET['solution_id']) <= 0 ) {
    echo '参数错误';
    exit(1);
}

$solutionId = intval($_GET['solution_id']);

// 查询该提交记录的用户ID
$sql = "select user_id, `result` from solution where solution_id=?";
$result = DbUtil::getInstance()->query($sql, $solutionId);
$posterId = null;
$resultId = 0;
if ( $result != null ) {
    $posterId = $result[0][0];
    $resultId = intval($result[0][1]);
}

$allowDel = false;
// 1. 管理员可删除任何提交记录
if ( isset($_SESSION['administrator']) ) {
    $allowDel = true;
}
else if ( isset($_SESSION['teacher']) ) {
    // 2-1. 教师允许删除自己的提交记录
    $teacherUid = $_SESSION['user_id'];
    if ( $posterId != null && $posterId == $teacherUid ) {
        $allowDel = true;
    }
    else if ( $posterId != null ){
        // 2-2. 教师允许删除自己学生的提交记录
        $sql = "select COUNT(*) from teacher_teach_stu where teacher_uid=? and stu_uid=?";
        $result = DbUtil::getInstance()->query($sql, $teacherUid, $posterId);
        $cnt = 0;
        if ( $result != null ) {
            $cnt = intval($result[0][0]);
        }
        if ($cnt > 0 ) {
            $allowDel = true;
        }
    }
}

if ( !$allowDel  ) {
    echo "<script>".
        "alert('不具备该权限,教师只允许删除自己的提交记录和所教学生的提交记录');".
        "history.go(-1);".
        "</script>";
    exit(0);
}

// 删除solution
$sql = "delete from `solution` where `solution_id`=?";
DbUtil::getInstance()->query($sql, $solutionId);

// 删除solution对应的代码
$sql = "delete from `source_code` where `solution_id`=?";
DbUtil::getInstance()->query($sql, $solutionId);

// 删除solution对应的代码
$sql = "delete from `source_code_user` where `solution_id`=?";
DbUtil::getInstance()->query($sql, $solutionId);

// 更新用户正确完成题目的数量(-1)
// $resultId == 4 表示提交的记录为AC的记录
if ( $resultId == 4 ) {
    $sql = "update users set solved=solved-1 where user_id=?";
    DbUtil::getInstance()->query($sql, $posterId);
}

// 删除solution对应的评测详情
$sql = "delete from `solution_detail` where `solution_id`=?";
DbUtil::getInstance()->query($sql, $solutionId);

$sql = "delete from `runtimeinfo` where `solution_id`=?";
DbUtil::getInstance()->query($sql, $solutionId);

// 1.更新table solution的自增起点值
$sql="select max(solution_id) FROM `solution`" ;
$result = DbUtil::getInstance()->query($sql);
$row = $result[0];
$max_id = $row[0];
$max_id++;
if( $max_id<1000 ) {
    $max_id = 1000;
}
// 若数组中存在比 $max_id大的记录，经测试该语句会失效
$sql="ALTER TABLE solution AUTO_INCREMENT = $max_id";
$result = DbUtil::getInstance()->query($sql);

// 2. 更新table solution_detail的自增起点值
$sql="select max(detail_id) FROM `solution_detail`" ;
$result = DbUtil::getInstance()->query($sql);
$row = $result[0];
$max_id = $row[0];
$max_id++;
$sql="ALTER TABLE solution_detail AUTO_INCREMENT = $max_id";
$result = DbUtil::getInstance()->query($sql);
?>

<script language=javascript>
    history.go(-1);
</script>